package com.nnnu.wsnackshop.controller.user;


import com.nnnu.wsnackshop.constant.MessageConstants;
import com.nnnu.wsnackshop.pojo.vo.SpuVO;
import com.nnnu.wsnackshop.result.Result;
import com.nnnu.wsnackshop.service.IProductSpuService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 产品SPU表 前端控制器
 * </p>
 *
 * @author zk
 * @since 2025-05-14
 */
@RestController("userProductSpuController")
@RequestMapping("/api/user/spu")
@Tag(name = "用户端-SPU接口")
@Slf4j
@RequiredArgsConstructor
public class ProductSpuController {
    private final IProductSpuService spuService;

    /**
     * 查询某商品下所有可用 SPU（不分页）
     * GET /api/user/spu/by-product/{productId}
     *
     * 缓存 key: "userSpuByProduct::product:{productId}"
     */
    @GetMapping("/by-product/{productId}")
    @Cacheable(cacheNames = "userSpuByProduct", key = "'product:' + #productId")
    public Result<List<SpuVO>> listSpuByProduct(@PathVariable("productId") Integer productId) {
        log.info("[用户端] 查询商品下所有 SPU: productId={}", productId);
        List<SpuVO> list = spuService.listSpuByProduct(productId);
        return Result.success(list, MessageConstants.SELECT_OK);
    }
}
